<template>
  <div>
    <myheader></myheader>
    <myswiper :listImg="advertsImg"></myswiper>
    <myroom :roomList="allRoom"></myroom>
  </div>
</template>
<script>
  import myheader from '../components/header.vue'
  import myswiper from '../components/swiperLoop.vue'
  import myroom from '../components/roomList.vue'
  export default {
    name: 'roomList',
    components: {
      myheader,
      myswiper,
      myroom
    },
    data () {
      return {
        advertsImg: '',
        allRoom: '',
        hasHeader: ''
      }
    },
    created () {
      document.documentElement.style.cssText += 'height: 100%!important;'
      document.body.style.cssText += ';background-color: #ffdf58!important; min-height: 100%; width: 100%;'
    },
    beforeMount () {
      this.$utils.setCookie('WAWA_TOKEN', this.$utils.getQueryStringByName('token'), 7)
      this.hasHeader = this.$utils.getQueryStringByName('token') ? this.$utils.getQueryStringByName('token') : this.$utils.getCookie('WAWA_TOKEN')
    },
    mounted () {
      this.roomList()
      this.adverts()
      this.userDetail()
      this.configInfoGet()
      this.shareInfo()
    },
    methods: {
      // 微信配置
      configInfoGet () {
        let localUrl = window.location.href
        var _this = this
        _this.$axios.get('https://www.likewawa.com/miaozhuapc/v1/wx/get/js/sdk?url=' + encodeURIComponent(localUrl)).then(function (response) {
          console.log(response)
          let appId = response.data.app_id
          let timestamp = response.data.config_timestamp
          let nonceStr = response.data.noncestr
          let signature = response.data.config_sign
          _this.config(appId, timestamp, nonceStr, signature)
        }).catch(function (response) {
          console.log(response)
        })
      },
      config (appId, timestamp, nonceStr, signature) {
        wx.config({
          debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
          appId: appId, // 必填，公众号的唯一标识
          timestamp: timestamp, // 必填，生成签名的时间戳
          nonceStr: nonceStr, // 必填，生成签名的随机串
          signature: signature, // 必填，签名
          jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline'] // 必填，需要使用的JS接口列表
        })
      },
      // 微信分享
      shareInfo () {
        // 分享给朋友
        wx.ready(function () {
          wx.onMenuShareAppMessage({
            title: '喵抓', // 分享标题
            desc: '实时直播抓娃娃，抓中送货到家哟', // 分享描述
            link: 'http://www.likewawa.com/mzweb/#/room-list', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: 'http://ojr9q26te.bkt.clouddn.com/share_icon_20180323171710.png', // 分享图标
            type: 'link', // 分享类型,music、video或link，不填默认为link
            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function () {
              // alert('share success')
              // 用户确认分享后执行的回调函数
            },
            cancel: function () {
              // alert('share error')
              // 用户取消分享后执行的回调函数
            }
          })
          // 分享到朋友圈
          wx.onMenuShareTimeline({
            title: '喵抓', // 分享标题
            link: 'http://www.likewawa.com/mzweb/#/room-list', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: 'http://ojr9q26te.bkt.clouddn.com/share_icon_20180323171710.png', // 分享图标
            success: function () {
              // alert('share success')
              // 用户确认分享后执行的回调函数
            },
            cancel: function () {
              // alert('share error')
              // 用户取消分享后执行的回调函数
            }
          })
        })
      },
      userDetail () {
        this.$http({
          method: 'get',
          url: 'https://www.likewawa.com/miaozhuapc/v1/user/getUserDetail',
          headers: {'WAWA_TOKEN': this.hasHeader},
          emulateJSON: true
        }).then(function (response) {
          response = JSON.parse(response.bodyText)
          if (response.status.code === '00000003') {
            top.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?' +
              'appid=wx890410ff186d98b7' +
              '&redirect_uri=https%3A%2F%2Fwww.likewawa.com%2Fmiaozhuapc%2Fv1%2Flogin%2Fh5login' +
              '&response_type=code' +
              '&scope=snsapi_userinfo' +
              '&state=room-list?1=1'
          } else {
            return false
          }
        }, function () {
          console.log('稍后重试')
        })
      },
      adverts () {
        this.$http({
          method: 'get',
          url: 'https://www.likewawa.com/miaozhuapc/v1/advert/getTypeAdvert',
          params: {'type': 1},
          emulateJSON: true
        }).then(function (response) {
          response = JSON.parse(response.bodyText)
          this.advertsImg = response.body
        }, function () {
          console.log('稍后重试')
        })
      },
      roomList () {
        this.$http({
          method: 'get',
          url: 'https://www.likewawa.com/miaozhuapc/v1/room/getRooms',
          credentials: false,
          emulateJSON: true
        }).then(function (response) {
          console.log(response)
          response = JSON.parse(response.bodyText)
          this.allRoom = response
        }, function () {
          console.log('稍后重试')
        })
      }
    },
    beforeDestroy () {
      document.documentElement.style.cssText += 'height: auto!important;'
      document.body.style.cssText += ';background-color: none; min-height: auto;'
    }
  }
</script>

<style scope lang="scss">
  html{
    height: 100%;
  }
</style>
